package com.byxy.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.byxy.entity.User;
import com.byxy.service.UserService;

@Controller
public class UserController {
	@Resource
	private UserService userService;

	/*
	 * struts action中的name 改为 RequestMapping 中 value 值，Session直接在参数中注入
	 * struts 若action中返回值类型为 redirect ,Controller 中也是redirect
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpSession session) {
		session.removeAttribute("user");
		return "redirect:/";
	}

	@RequestMapping(value = "doLogin", method = RequestMethod.POST)
	public String doLogin(String name, String pwd,HttpSession session) {
		User u = userService.findByLoginNameAndPassword(name, pwd);
		if (u == null) {
			session.removeAttribute("user");
			return "redirect:/";
		} else {
			session.setAttribute("user", u);
			return "redirect:/main.jsp";
		}
	}
}
